using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using TccAg.VO;

namespace TccAg.DA
{
    public class GradecurricularDA
    {
        private String queryInsert = "INSERT INTO gradecurricular( PERACAD, CURSO, ATIVA) VALUES( @peracad, @curso, @ativa)";
        private String queryUpdate = "UPDATE gradecurricular SET peracad = @peracad, curso = @curso, ativa = @ativa WHERE ID = @id";
        private String queryDelete = "DELETE FROM gradecurricular WHERE ID = @id";
        private String queryLoadAll = "SELECT  id, peracad, curso, ativa FROM gradecurricular";
        private String queryLoadByPk = "SELECT  id, peracad, curso, ativa FROM gradecurricular WHERE ID = @id";
        private String queryLoadByFk = "SELECT  id, peracad, curso, ativa FROM gradecurricular WHERE PERACAD = @peracad, CURSO = @curso";
        private SqlConnection conexao = new SqlConnection("Pegar conexão");

        //INSERIR 
        public int inserir(GradecurricularVO gradecurricular)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryInsert;

                if (gradecurricular.Peracad != 0)
                        cmd.Parameters.Add(new SqlParameter("@peracad", (object) gradecurricular.Peracad));
                else
                        cmd.Parameters.Add(new SqlParameter("@peracad", (object) DBNull.Value));

                if (gradecurricular.Curso.Id != 0)
                        cmd.Parameters.Add(new SqlParameter("@curso", (object) gradecurricular.Curso.Id));
                else
                        cmd.Parameters.Add(new SqlParameter("@curso", (object) DBNull.Value));

                if (gradecurricular.Ativa != 0)
                        cmd.Parameters.Add(new SqlParameter("@ativa", (object) gradecurricular.Ativa));
                else
                        cmd.Parameters.Add(new SqlParameter("@ativa", (object) DBNull.Value));

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //ATUALIZAR 
        public int atualizar(GradecurricularVO gradecurricular)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                if (gradecurricular.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)gradecurricular.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryUpdate;

                if (gradecurricular.Peracad != 0)
                        cmd.Parameters.Add(new SqlParameter("@peracad", (object) gradecurricular.Peracad));
                else
                        cmd.Parameters.Add(new SqlParameter("@peracad", (object) DBNull.Value));
                if (gradecurricular.Curso.Id != 0)
                        cmd.Parameters.Add(new SqlParameter("@curso", (object) gradecurricular.Curso.Id));
                else
                        cmd.Parameters.Add(new SqlParameter("@curso", (object) DBNull.Value));
                if (gradecurricular.Ativa != 0)
                        cmd.Parameters.Add(new SqlParameter("@ativa", (object) gradecurricular.Ativa));
                else
                        cmd.Parameters.Add(new SqlParameter("@ativa", (object) DBNull.Value));
                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //EXCLUIR 
        public int excluir(GradecurricularVO gradecurricular)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryDelete;

                if (gradecurricular.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)gradecurricular.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD ALL 
        public List<GradecurricularVO> loadAll()
        {
            try
            {
                List<GradecurricularVO> objRetorno = new List<GradecurricularVO>();
                GradecurricularVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objTemp = new GradecurricularVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["peracad"] != DBNull.Value)
                           objTemp.Peracad = (int)dr["peracad"];

                        if (dr["curso"] != DBNull.Value)
                        {
                            CursoVO parametro = new CursoVO();
                            parametro.Id = (int)dr["curso"];
                            objTemp.Curso = new CursoDA().loadByPk(parametro);
                        }

                        if (dr["ativa"] != DBNull.Value)
                           objTemp.Ativa = (int)dr["ativa"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY PK - Passa um objeto do tipo VO para em casos onde usar chave primária composta ter essas propriedades preenchidas
        public GradecurricularVO loadByPk(GradecurricularVO gradecurricular)
        {
            try
            {
                GradecurricularVO objRetorno = null;
                GradecurricularVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (gradecurricular.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)gradecurricular.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByPk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new GradecurricularVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["peracad"] != DBNull.Value)
                           objTemp.Peracad = (int)dr["peracad"];

                        if (dr["curso"] != DBNull.Value)
                        {
                            CursoVO parametro = new CursoVO();
                            parametro.Id = (int)dr["curso"];
                            objTemp.Curso = new CursoDA().loadByPk(parametro);
                        }

                        if (dr["ativa"] != DBNull.Value)
                           objTemp.Ativa = (int)dr["ativa"];

                        objRetorno = objTemp;
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY FK - Passa um objeto do tipo VO para em casos onde usar chave estrangeira composta ter essas propriedades preenchidas
        public List<GradecurricularVO> loadByFk(GradecurricularVO gradecurricular)
        {
            try
            {
                List<GradecurricularVO> objRetorno = new List<GradecurricularVO>();
                GradecurricularVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (gradecurricular.Peracad != 0)
                    cmd.Parameters.Add(new SqlParameter("@peracad", (object)gradecurricular.Peracad));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                if (gradecurricular.Curso.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@curso", (object)gradecurricular.Curso.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByFk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new GradecurricularVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["peracad"] != DBNull.Value)
                           objTemp.Peracad = (int)dr["peracad"];

                        if (dr["curso"] != DBNull.Value)
                        {
                            CursoVO parametro = new CursoVO();
                            parametro.Id = (int)dr["curso"];
                            objTemp.Curso = new CursoDA().loadByPk(parametro);
                        }

                        if (dr["ativa"] != DBNull.Value)
                           objTemp.Ativa = (int)dr["ativa"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //DATATABLE LOAD ALL 
        public DataTable dataTableLoadAll()
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                SqlDataAdapter dtAdpt = new SqlDataAdapter(cmd);
                DataTable dtTbl = new DataTable("tab_gradecurricular");

                dtAdpt.Fill(dtTbl);
                return dtTbl;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

    }
}
